
$(function(){

	function GetQueryString (name) { //获取指定name的url参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return (r[2]);
        return null;
    }
    function Toast (txt){
        var toastHtml = '<div class="tk_info" id="tkInfo"></div>';
        var toast = $('body #tkInfo');
        if (toast.length) {
            toast.html(txt).show();
        }else{
            $('body').append(toastHtml);
            $('body #tkInfo').html(txt).show();
        }
        setTimeout(function(){
          $('body #tkInfo').hide();
        },2000)
    }


    const userId = GetQueryString('userId');
    const token = GetQueryString('token');
    //GetQueryString('invokeDomain')
    let invokeDomain = GetQueryString('invokeDomain');
    let host = 'http://'+invokeDomain;
    let ifrom = GetQueryString('ifrom'),
        idfa = GetQueryString('idfa'),
        channel = GetQueryString('channel'),
        version = GetQueryString('version');

    let loginState = false;
    var uqInviteCode;
    let is_bind_wx = '';
    let showSignAlert = '';
    let dataObj;
    dataObj ='userId='+userId+'&token='+token+'&invokeDomain='+invokeDomain+'&code='+uqInviteCode;
    let countDown = '';
    if (userId&&token) {
        loginState = true;
    }

    //展开按钮是否出现
    function p_show(len) {
        if (len>2) {
            $('#newTask .lists:gt(1)').hide();
            $('.show').show();
            $('.show').on('click',function(){
                if ($('.icon_jt').hasClass('up')) {
                    $('#newTask .lists:gt(1)').hide(300);
                    $('.icon_jt').removeClass('up')
                }else{
                    $('#newTask .lists').show(300);
                    $('.icon_jt').addClass('up')
                }
                
                
            })
        }
    }
    goLogin = function() {//去登录
        // Toast('goLogin')
        H5brigeAPP.MJLogin()
    }
    bindWx = function(){//去绑定微信
        // Toast('bindWx')
        H5brigeAPP.MJWXBinding()
    }
    goIndex = function (){ //去首页
        // Toast('goIndex')
        H5brigeAPP.MJGoHome()
    }
    // goUser = function(){ //去个人中心
    //     Toast('goUser')
    // }
    goEmail = function (){ //打开编辑资料页
        // Toast('goEmail')
        H5brigeAPP.MJEdit();
       
    }
    getCompleteTask = function(ID){//领取任务奖励
        $.ajax({
            type:'get',
            dataType: 'json',
            url:host+'/system/api/appuser/completeTask',
            data: {
                'userId':userId,
                'token':token,
                'utId':ID
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const data = v.data
                if (code == 0) {
                   Toast(v.msg);
                   setTimeout(function(){
                    window.location.reload();
                   },1500)
                }else{
                    Toast(v.msg);
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
            }
        })
    }
    getrReceiveVideoTask = function (){//领取观影任务奖励
        $.ajax({
            type:'get',
            dataType: 'json',
            url:host+'/system/api/appuser/receiveVideoTask',
            data: {
                'userId':userId,
                'token':token,
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const data = v.data
                if (code == 0) {
                   Toast(v.msg);
                }else{
                    Toast(v.msg);
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
            }
        })
    }
    getWithdrawUrl = function (obj1,obj2){ //获取提现地址
        $.ajax({
            type:'get',
            dataType: 'json',
            url:host+'/profile/conf/serviceH6.json',
            // url:'http://yokq.shnongc.cn//serviceH5.json',
            data: {
                'userId':userId,
                'token':token,
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const data = v.data
                if (code == 0) {
                    let txUrl = '',thirdUrl='';
                    
                    if (data.tx.url.indexOf('?')>0) 
                        txUrl = data.tx.url+'&'+dataObj
                    else
                        txUrl = data.tx.url+'?'+dataObj;
                    if (data.third.url.indexOf('?')>0) 
                        thirdUrl = data.third.url+'&'+dataObj;
                    else
                        thirdUrl = data.third.url+'?'+dataObj;
                    obj1.attr('href',txUrl)
                    obj2.attr('href',thirdUrl)
                }else{
                    Toast(v.msg);
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
            }
        })
    }
    function qd(userId,token) { //签到
        $.ajax({
            type:'get',
            dataType: 'json',
            url:host+'/system/api/appuser/signInfo',
            data: {
                'userId':userId,
                'token':token,
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const data = v.data
                if (code == 0) {
                    let coins = data.coins,to = data.to,htmls = [];
                    $('#tkVal').html(coins[to-1]);
                    coins.forEach(function(item,index){
                        let className = '',p='未签到';
                        if (index < to) {
                            className = 'hover';
                            p = '已签到';
                        }
                        if (index == to) {
                            className = 'hover2'
                        }
                        var html = '<li class="'+className+'">'+
                            '<img src="./image/img_qd.jpg">'+
                            '<span>'+item+'</span><p>'+p+'</p>'+
                        '</li>';
                        htmls.push(html)
                   })
                    $('.tkcom-qd-com ul').html(htmls.join(''))
                }else{
                    Toast(v.msg);
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
            }
        })
    }
    showTK = function (){
        $('#tkbox').show()
    }
    hideTK = function (){
        $('#tkbox').hide()
    }
    function temp(arr){
        let arrs = [];
        arr.forEach(function(item,index) {
            let className = 'blue',url = item.url+dataObj+'&utId='+item.utId,aHtml='';
            if (item.status == 2) { //待领取
                aHtml = '<a href="javascript:void(0);" class="red"  onClick="getCompleteTask('+item.utId+')">'+item.showText+'</a>';
            }else{
                if (item.opt == 0) {// 0-无操作 1-显示签到信息 2-查询是否绑定微信 3-观影任务 4-邮箱
                    aHtml = '<a href="'+url+'" class="'+className+'">'+item.showText+'</a>';
                }else if (item.opt == 1) {
                    $('.tkcom-qd-btn a').attr('href',url);

                    aHtml = '<a href="javascript:void(0);" class="blue" onClick="showTK()">'+item.showText+'</a>';
                }else if (item.opt==2) {
                    if (is_bind_wx !=1 ) {
                        aHtml = '<a href="javascript:void(0);" class="blue" onClick="bindWx()">'+item.showText+'</a>';
                    }else{
                        aHtml = '<a href="'+url+'" class="'+className+'">'+item.showText+'</a>';
                    }
                }else if(item.opt == 3) {
                    if (item.status == 0) {//去首页观看
                        aHtml = '<a href="javascript:void(0);" class="blue" onclick="goIndex()">'+item.showText+'</a>';
                    }else{
                       countDown = item.countDown;
                       let html = '';
                       if (countDown<10) {
                            html = '00:0'+countDown
                        }
                        if (countDown>=60) {
                            let ys =((countDown%60)<=9)?('0'+countDown%60):(ountDown%60);
                            let ss = ((countDown/60)<=9?('0'+countDown/60):(ountDown/60))
                            html = ss+':'+ys
                        }
                        aHtml = '<a href="javascript:void(0);" class="gary" id="countDown">'+html+'</a>';
                    }
                }else if(item.opt == 4){
                    aHtml = '<a href="javascript:void(0);" class="blue" onclick=goEmail()>'+item.showText+'</a>';
                }
            }
            
            let html = '<div class="lists">'+
                '<div class="left">'+
                '    <div class="top">'+
                '       <h3>'+item.title+'</h3>'+
                '        <span class="icon_red"></span>'+
                '        <font>+'+item.money+'</font>'+
                '    </div>'+
                '    <p>'+item.content+'</p>'+
                '</div>'+
                '<div class="rigtht">'+aHtml+'</div>'+
            '</div>';
            arrs.push(html)
        })
        return arrs
    }
    function getInfo(userId,token) {//获取任务首页信息
        $.ajax({
            type:'get',
            dataType: 'json',
            url:host+'/system/api/appuser/task',
            data: {
                'userId':userId,
                'token':token,
                'channel':channel,
                'idfa':idfa,
                'ifrom':ifrom
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const data = v.data
                if (code == 0) {
                    uqInviteCode = data.uqInviteCode
                    is_bind_wx = data.is_bind_wx
                    showSignAlert = data.showSignAlert
                    dataObj ='userId='+userId+'&token='+token+'&invokeDomain='+invokeDomain+'&code='+uqInviteCode;
                    $('#balance').html(data.balance)
                    $('#cmsMoney').html(data.cmsMoney)
                    // if (is_bind_wx==1) {//已绑定微信
                        getWithdrawUrl($('#withdraw1'),$('#withdraw2'));
                    // }else{
                    //    $('#withdraw1').on('click',bindWx)
                    //    $('#withdraw2').on('click',bindWx) 
                    // }
                    //新手任务
                    if (data.daysLimit==0||!data.fqs||data.fqs.length==0) {
                        $('#newTask').hide()
                    }else{
                        $('#daysLimit').html('剩余 '+data.daysLimit+' 天');
                        let html = temp(data.fqs);
                        $('.newTaskUl').html(html.join(''))
                    }
                    //日常任务
                    let htmlRC = temp(data.dqs);
                    $('.newTaskUl2').html(htmlRC.join(''))
                    //banner
                    let bannerData = data.ads;
                    let wid = 2.64;
                    if (bannerData.length >0) {
                        $('.ad').show(); 
                        bannerData.forEach(function(item,index){
                            let aHtml = '<a href="'+item.url+'"><img src="'+image+'"></a>';
                            $('.ad .banner').append(aHtml);
                        })
                        $('.ad .banner').css('width',bannerData.length*wid+'rem')
                    }
                }else{
                    Toast(v.msg);
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
            }
        })
    }
    function info() {//初使化
        if (!loginState) {//未登录
            $('.rigtht a').on('click',goLogin);
            $('.withdraw-list a').on('click',goLogin);
            p_show(4)
        }else{
            qd(userId,token)
           getInfo(userId,token)
           //倒计时
           if (countDown) {
            let timer = null;
            let html = '';
            timer = setInterval(function(){
                countDown--;
                if (countDown==-1) {
                    getrReceiveVideoTask()
                    countDown = 6 *60
                }
                if (countDown<10) {
                    html = '00:0'+countDown
                }
                if (countDown>=60) {
                    let ys =((countDown%60)<=9)?('0'+countDown%60):(ountDown%60);
                    let ss = ((countDown/60)<=9?('0'+countDown/60):(ountDown/60))
                    html = ss+':'+ys
                }
                $('#countDown').html(html);
            },1000*60)
           }
        }
    }
    info()
    //foot
    // $('.foot a').on('click',function(){

    //     let index = $(this).index();
    //     if (index == 0) {
    //         window.webkit.messageHandlers.MJGoHome.postMessage(null) 
    //     }
    //     if (index == 1) {
    //         window.location.reload();
    //     }
    //     if (index == 2) {
    //         if (!loginState) {//没有登录
    //             goLogin()
    //         }else{//去个人中心页
    //             goUser()
    //         }
    //     }
    // })
    //tk
    $('.tkcom-qd-close').on('click',hideTK)
})